﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WeMakeSP.SiteManagement.UI.Views;
using WeMakeSP.SiteManagement.UI.Models;
using WeMakeSP.Platform.SharePoint.BusinessEntities;
using WeMakeSP.Platform.SharePoint.Web.UI.Presenters;

namespace WeMakeSP.SiteManagement.UI.Presenters
{
    /// <summary>
    /// The presenter for the site hierarchy user interface.
    /// </summary>
    public class SiteHierarchyPresenter : PresenterPageBase<ISiteHierarchyView>, ISiteHierarchyPresenter
    {
        #region Constructors

        /// <summary>
        /// Initializes a new instance of the <see cref="SiteHierarchyPresenter"/> class.
        /// </summary>
        /// <param name="view">The site hierarchy view.</param>
        public SiteHierarchyPresenter(ISiteHierarchyView view, ISiteHierarchyModel model)
            : base(view)
        {
            // Guard.

            if (model == null)
            {
                throw new ArgumentNullException("model");
            }

            // Set the model.

            this.Model = model;
        }

        #endregion

        #region Protected Override Methods

        /// <summary>
        /// Called when [view initialized].
        /// </summary>
        public override void OnViewInitialized()
        {
            base.OnViewInitialized();

            // Set the root web id.

            this.View.RootWebId = 
                this.View.Web.ID;
        }

        /// <summary>
        /// Called when [view loaded].
        /// </summary>
        public override void OnViewLoaded()
        {
            base.OnViewLoaded();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets the model.
        /// </summary>
        /// <value>The model.</value>
        public ISiteHierarchyModel Model
        {
            get;
            private set;
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// Processes the request.
        /// </summary>
        public void ProcessRequest()
        {
            // Get the selected webs.

            List<Guid> selectedWebIds = 
                this.View.SelectedWebIds;

            // Anything to process.

            if (selectedWebIds.Count > 0)
            {
                // Get the current web configuration.

                SPWebConfiguration webConfiguration =
                    this.Model.GetWebConfiguration(string.Empty);

                // Theme.

                bool inheritTheme = 
                    this.View.InheritTheme;

                if (inheritTheme == true)
                {
                    this.Model.UpdateWebsTheme(selectedWebIds, webConfiguration.Theme);
                }

                // Master Page.

                bool inheritMasterPage =
                    this.View.InheritMasterPage;

                if (inheritMasterPage == true)
                {
                    this.Model.UpdateWebsMasterPage(
                        selectedWebIds,
                        new SPMasterPage()
                        {
                            CustomMasterUrl = webConfiguration.CustomMasterUrl,
                            MasterUrl = webConfiguration.MasterUrl
                        });
                }

                // Web Logo.

                bool inheritLogo = 
                    this.View.InheritLogo;

                if (inheritLogo == true)
                {
                    this.Model.UpdateWebsLogo(
                        selectedWebIds,
                        new SPWebLogo()
                        {
                            Description = webConfiguration.LogoDescription,
                            Url = webConfiguration.LogoUrl
                        });
                }

                // Redirect to the settings page.

                this.View.RedirectToSettingsPage();
            }
        }

        #endregion
    }
}
